package bTree.LeetCode;

import java.util.HashSet;
import java.util.Set;

public class 无重复字符的最长子串 {
    public int lengthOfLongestSubstring(String s) {
        Set<Character>set=new HashSet<>();
        int res=1;
        int path=1;
        int length = s.length();
        if (0==length)return 0;
        int left=0,right=1;
        set.add(s.charAt(0));
        while (right<length){
            char c = s.charAt(right);
            if (!set.contains(c)){
                set.add(c);
                path++;
                right++;
                res=Math.max(res,path);
            }else{
                while (s.charAt(left)!=c){
                    set.remove(s.charAt(left));
                    left++;
                    path--;
                }
                left++;
                right++;
            }
        }
        return res;
    }
}